﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///Login 的摘要说明
/// </summary>
public class Login
{
    public Login()
    {
    }

    public void SetCheckCode(Site site, Shove.Web.UI.ShoveCheckCode CheckCode)
    {
        switch (PublicFunction.GetOptionsAsInt(("Opt_CheckCodeCharset"), 4))
        {
            //0 不使用验证码  1 字母 2小写字母 3 大写字母 4 数字 5 所有字符
            case 0:
                CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.All;
                break;

            case 1:
                CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyLetter;
                break;

            case 2:
                CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyLetterLower;
                break;

            case 3:
                CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyLetterUpper;
                break;

            case 4:
                CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyNumeric;
                break;

            default:
                CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.All;
                break;
        }
    }

    public int LoginSubmit(Page page, Site site, string CustomerNO, string Password, string CheckCode, ref string ReturnDescription)
    {
        return LoginSubmit(page, site, CustomerNO, Password, CheckCode, "ShoveWebUI_CheckCode_CheckCode", ref ReturnDescription);
    }

    public int LoginSubmit(Page page, Site site, string CustomerNO, string Password, ref string ReturnDescription)
    {
        ReturnDescription = "";

        if (!Shove._Security.Encrypt.ValidUserAndPassword(CustomerNO, Password))
        {
            return LoginSubmit(page, site, ref ReturnDescription);
        }

        CustomerNO = CustomerNO.Trim();
        Password = Password.Trim();

        if ((CustomerNO == "") || (Password == ""))
        {
            ReturnDescription = "用户名和密码都不能为空";

            return -1;
        }      

        System.Threading.Thread.Sleep(500);

        Customer customer = new Customer(site.ID);
        customer.CustomerNO = CustomerNO;
        customer.Password1 = Password;

        return customer.Login(ref ReturnDescription);
    }

    public int LoginSubmit(Page page, Site site, string CustomerNO, string Password, string CheckCode, string CheckCodeKey, ref string ReturnDescription)
    {
        ReturnDescription = "";

        if (!Shove._Security.Encrypt.ValidUserAndPassword(CustomerNO, Password))
        {
            return LoginSubmit(page, site, ref ReturnDescription);
        }

        bool Opt_isUseCheckCode = site.SiteOptions["Opt_isUseCheckCode"].ToBoolean(true);

        CustomerNO = CustomerNO.Trim();
        Password = Password.Trim();

        if ((CustomerNO == "") || (Password == ""))
        {
            ReturnDescription = "用户名和密码都不能为空";

            return -1;
        }

        if (CheckCodeKey == "")
        {
            CheckCodeKey = "ShoveWebUI_CheckCode_CheckCode";
        }

        string SessionCheckCode = "";

        try
        {
            SessionCheckCode = System.Web.HttpContext.Current.Session[CheckCodeKey].ToString();
        }
        catch { }

        if ((Opt_isUseCheckCode) && (SessionCheckCode.ToUpper() != CheckCode.ToUpper()))
        {
            ReturnDescription = "验证码输入错误";

            return -2;
        }

        System.Threading.Thread.Sleep(500);

        Customer customer = new Customer(site.ID);
        customer.CustomerNO = CustomerNO;
        customer.Password1 = Password;

        return customer.Login(ref ReturnDescription);
    }

    private int LoginSubmit(Page page, Site site, ref string ReturnDescription)
    {
        ReturnDescription = "";

        DataTable dt = DAL.Functions.F_GetSiteAdministrators(site.ID);

        if ((dt == null) || (dt.Rows.Count < 1))
        {
            ReturnDescription = "数据读写错误";

            return -3;
        }

        Customer customer = new Customer(site.ID)[site.ID, dt.Rows[0]["CustomerNO"].ToString()];

        if (customer == null)
        {
            ReturnDescription = "数据读写错误";

            return -4;
        }

        return customer.LoginDirect(ref ReturnDescription);
    }

    public void GoToRequestLoginPage(string DefaultPage)
    {
        string RequestLoginPage = Shove._Web.Utility.GetRequest("RequestLoginPage");

        if (RequestLoginPage == null)
        {
            RequestLoginPage = "";
        }

        if (RequestLoginPage != "")
        {
            if (RequestLoginPage.StartsWith("Page/") || RequestLoginPage.StartsWith("BalanceCenter/") || RequestLoginPage.StartsWith("FinanceCenter/") || RequestLoginPage.StartsWith("SystemManage/"))
            {
                System.Web.HttpContext.Current.Response.Write("<script language='javascript'>window.top.location.href='index.aspx?SubPage=" + System.Web.HttpUtility.UrlEncode(RequestLoginPage) + "'</script>");

                return;
            }

            System.Web.HttpContext.Current.Response.Redirect(RequestLoginPage, true);
        }        
        else
        {
            if (DefaultPage.Trim() == "")
            {
                return;
            }

            System.Web.HttpContext.Current.Response.Redirect(DefaultPage, true);
        }
    }

}